package chapter7.eg1;

import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.junit.Test;

public class TaskExecutorExample {
  @Test
  public void testExecuteOneTask() throws InterruptedException {
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);
    executor.schedule(() -> System.out.println("10 seconds later"), 10, TimeUnit.SECONDS);
    TimeUnit.SECONDS.sleep(15);
  }

  @Test
  public void testEventLoopExecutor() throws InterruptedException {
    Channel channel = new NioServerSocketChannel();
    EventLoopGroup group = new NioEventLoopGroup();
    group.register(channel);
    channel.eventLoop()
        .schedule(() -> System.out.println("10 seconds later"), 10, TimeUnit.SECONDS);
    TimeUnit.SECONDS.sleep(15);
  }

  @Test
  public void testEventLoopFixRateExecutor() throws InterruptedException {
    EventLoopGroup group = new NioEventLoopGroup();
    group.scheduleAtFixedRate(() -> System.out.println("Run Every 5 Seconds"), 0, 5,
        TimeUnit.SECONDS);
    TimeUnit.SECONDS.sleep(30);
  }

}
